#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   grace_in.py
@Time    :   2020/11/27
@Author  :   wenke wang
@Version :   1.0
@Desc    :   GRACE缺血评估(入院)
'''

# here put the import lib
import sys, os, json
from decimal import Decimal
from grace_in_eval_param import GraceInHospitalEvalParam

def get_default_settings():
    """
    获取默认的评分配置
    """
    default_setting = None

    setting_path = os.path.join(sys.path[0], "grace_in_score_setting.json")
    with open(setting_path, "r", encoding="utf-8") as fp:
        default_setting = json.load(fp)
    
    return default_setting

def evaluate(param, score_settings = None):
    """
    GRACE缺血评估(入院)
    """
    if not score_settings:
        score_settings = get_default_settings()

    total_score = 0
    # killip得分
    total_score = total_score + [k["score"] for k in score_settings["killip"] if k["val"] == param.killip][0]
    # 收缩压得分
    total_score = total_score + [s["score"] for s in score_settings["sbp"] if s["low"] <= param.sbp and s["high"] > param.sbp][0]
    # 心率得分
    total_score = total_score + [h["score"] for h in score_settings["hp"] if h["low"] <= param.hp and h["high"] > param.hp][0]
    # 年龄得分
    total_score = total_score + [a["score"] for a in score_settings["age"] if a["low"] <= param.age and a["high"] > param.age][0]
    # 血肌酐得分
    total_score = total_score + [a["score"] for a in score_settings["scr"] if Decimal(str(a["low"])) <= Decimal(param.scr) and Decimal(str(a["high"])) > Decimal(param.scr)][0]
    # 院前心脏骤停得分
    total_score = total_score + [k["score"] for k in score_settings["sca"] if k["val"] == param.sca][0]
    # ST段下移得分
    total_score = total_score + [k["score"] for k in score_settings["std"] if k["val"] == param.std][0]
    # 心肌酶升高得分
    total_score = total_score + [k["score"] for k in score_settings["meup"] if k["val"] == param.meup][0]

    risk = [r["val"] for r in score_settings["risk"] if r["low"] < total_score and r["high"] >= total_score][0]

    return { "score": total_score, "risk": risk }

if __name__ == "__main__":
    param = GraceInHospitalEvalParam(68, 2, 168, 78, 0.96, 1, 0, 1)
    result = evaluate(param)
    print(result)

